my_libs\db\cmd/
create.rs

1// src/db/cmd/create.rs
2
3use crate::db::connect::DatabaseConnection;
4use surrealdb::Result;
5use uuid::Uuid;
6
7pub struct Creator<'a> {
8    db: &'a DatabaseConnection,
9}
10
11impl<'a> Creator<'a> {
12    pub fn new(db: &'a DatabaseConnection) -> Self {
13        Self { db }
14    }
15
16    // 📚 EDU: &str to "pożyczony string" (lekki), String to "własny string" (cięższy).
17    // Do argumentów funkcji zazwyczaj lepiej brać &str.
18    pub async fn add_cat(&self, imie: &str, kolor: &str) -> Result<String> {
19        let new_id = Uuid::now_v7();
20
21        // format! działa jak template string w TS: `CREATE ... ${imie}`
22        let sql = format!(
23            "CREATE kot:`{}` SET imie = '{}', kolor = '{}', zrodlo = 'Editor Window';",
24            new_id, imie, kolor
25        );
26
27        // Wykonujemy zapytanie
28        self.db.client.query(&sql).await?;
29
30        Ok(new_id.to_string())
31    }
32}